import {onPullDownRefresh, onReachBottom, onLoad, onShow, onUnload} from '@dcloudio/uni-app'
import {ref, reactive} from 'vue'
import { checkLogin } from '@/util/middleware'

export const useList = (serviceApi, params = {}) => {
    let info = reactive({
        isLoading: false, // 接口请求状态
        loadStatus: 'loadmore', // loadmore加载效果
        form: {
            page: 1, // 如需外部控制，则params传page和limit即可
            limit: 20,
        },
        list: [], // 数据
        total: 0, // 总条数
    })
    const initForm = async () => {
      info.form = {
        page: 1,
        limit: 20,
      }
      info.form = Object.assign({}, info.form, params)
      info.loadStatus = 'loadmore'
    }

    const getList = async (type = 'continue') => {
      info.form = Object.assign({}, info.form, params)
      if (info.isLoading) {
        return false
      }
      if (type != 'continue') {
        await initForm()
      }
      if (info.loadStatus == 'nomore') {
        return false
      }
      info.isLoading = true
      info.loadStatus = 'loading'

        if (type != 'continue') {
            info.list = []
        }
      const res = await serviceApi(info.form)
      info.isLoading = false
      setTimeout(() => {
          uni.stopPullDownRefresh()
      }, 300)
      if (res.code == 1) {
          info.list.push(...res.data.list)
        info.total = res.data.total
        if (res.data.list.length > 0) {
            info.form.page++
        }
        if (info.list.length >= info.total) {
          info.loadStatus = 'nomore'
        } else {
          info.loadStatus = 'loadmore'
        }
      }
    }

    onPullDownRefresh(() => {
        getList('init')
    })

    onReachBottom(() => {
        getList()
    })
    return {info, getList}
}

export const useRequest = (serviceApi, params = {}, initParams = {}, isTip = true) => {
    let info = reactive({
        form: {},
    })
    let isLoading = ref(false)
    let isSuccess = ref(false)
    const request = async () => {
        info.form = Object.assign({}, info.form, params)
        if (isLoading.value) {
            return false
        }
        isSuccess.value = false
        isLoading.value = true
        const res = await serviceApi(info.form)
        isLoading.value = false
        if (res.code == 1) {
            Object.assign(info, res)
            isSuccess.value = true
            if (isTip) {
                uni.showToast({
                    title: res.msg,
                    icon: 'none',
                })
            }
            Object.assign(params, initParams)
        }
    }
    return {isLoading, request, isSuccess, info}
}

export const useCheckLoginOnShow = (fn = null) => {
	onShow(async () => {
		checkLogin()
		if (fn) {
			fn()
		}
	})
}

export const useCheckLoginOnLoad = (fn = null) => {
	onLoad(async (opt) => {
		checkLogin()
        if (fn) {
            fn(opt)
        }
	})
}

export const useOnLoad = (fn = null) => {
	onLoad(async (opt) => {
		if (fn) {
			fn(opt)
		}
	})
}
export const useOnUnLoad = (fn = null) => {
	onUnload(async () => {
		if (fn) {
			fn()
		}
	})
}

export const useOnPullDownRefresh = (fn = null) => {
	onPullDownRefresh(() => {
		if (fn) {
			fn()
		}
	})
}
